<html>
<head><meta charset="utf-8"><title>Is there some kind of `is_callable(ty: &amp;Ty) -&gt; bool`? · t-compiler/help · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/index.html">t-compiler/help</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Is.20there.20some.20kind.20of.20.60is_callable(ty.3A.20.26Ty).20-.3E.20bool.60.3F.html">Is there some kind of `is_callable(ty: &amp;Ty) -&gt; bool`?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="217498108"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Is%20there%20some%20kind%20of%20%60is_callable%28ty%3A%20%26Ty%29%20-%3E%20bool%60%3F/near/217498108" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Robin Moussu <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Is.20there.20some.20kind.20of.20.60is_callable(ty.3A.20.26Ty).20-.3E.20bool.60.3F.html#217498108">(Nov 21 2020 at 11:38)</a>:</h4>
<p>I would like to know if a constant is a callable. I have access to  its <a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/struct.TyS.html">type</a>. Is it enough to test <code>if ty.is_fn() || ty.is_fn_ptr() || ty.is_closure()</code> or did I forgot something?</p>



<a name="217501576"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Is%20there%20some%20kind%20of%20%60is_callable%28ty%3A%20%26Ty%29%20-%3E%20bool%60%3F/near/217501576" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Is.20there.20some.20kind.20of.20.60is_callable(ty.3A.20.26Ty).20-.3E.20bool.60.3F.html#217501576">(Nov 21 2020 at 13:18)</a>:</h4>
<p>I would go with that for now and fix the bugs later ;)</p>



<a name="217501581"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Is%20there%20some%20kind%20of%20%60is_callable%28ty%3A%20%26Ty%29%20-%3E%20bool%60%3F/near/217501581" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Is.20there.20some.20kind.20of.20.60is_callable(ty.3A.20.26Ty).20-.3E.20bool.60.3F.html#217501581">(Nov 21 2020 at 13:18)</a>:</h4>
<p>I think that might miss explicit implementations of Fn and FnMut but those are also very unstable</p>



<a name="217501757"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Is%20there%20some%20kind%20of%20%60is_callable%28ty%3A%20%26Ty%29%20-%3E%20bool%60%3F/near/217501757" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Robin Moussu <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Is.20there.20some.20kind.20of.20.60is_callable(ty.3A.20.26Ty).20-.3E.20bool.60.3F.html#217501757">(Nov 21 2020 at 13:24)</a>:</h4>
<p>The issue is that I really think that knowing that there is a bug is going to be extremely hard (a call graph with a missing dependency out of 10k dependency really look like a non buggy call graph!).</p>



<a name="217501815"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Is%20there%20some%20kind%20of%20%60is_callable%28ty%3A%20%26Ty%29%20-%3E%20bool%60%3F/near/217501815" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Robin Moussu <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Is.20there.20some.20kind.20of.20.60is_callable(ty.3A.20.26Ty).20-.3E.20bool.60.3F.html#217501815">(Nov 21 2020 at 13:27)</a>:</h4>
<p>I hope that my test harness will be robust enough</p>



<a name="217528495"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Is%20there%20some%20kind%20of%20%60is_callable%28ty%3A%20%26Ty%29%20-%3E%20bool%60%3F/near/217528495" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Is.20there.20some.20kind.20of.20.60is_callable(ty.3A.20.26Ty).20-.3E.20bool.60.3F.html#217528495">(Nov 22 2020 at 01:34)</a>:</h4>
<p>Perhaps add an <code>is_callable</code> function (perhaps as a method on <code>Ty</code>) that encapsulates <code>ty.is_fn() || ty.is_fn_ptr() || ty.is_closure()</code> so that we can fix any bugs in one place?</p>



<a name="217540448"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Is%20there%20some%20kind%20of%20%60is_callable%28ty%3A%20%26Ty%29%20-%3E%20bool%60%3F/near/217540448" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Robin Moussu <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Is.20there.20some.20kind.20of.20.60is_callable(ty.3A.20.26Ty).20-.3E.20bool.60.3F.html#217540448">(Nov 22 2020 at 08:09)</a>:</h4>
<p>Done: <a href="https://github.com/rust-lang/rust/pull/79290">https://github.com/rust-lang/rust/pull/79290</a></p>



<a name="217548865"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Is%20there%20some%20kind%20of%20%60is_callable%28ty%3A%20%26Ty%29%20-%3E%20bool%60%3F/near/217548865" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Is.20there.20some.20kind.20of.20.60is_callable(ty.3A.20.26Ty).20-.3E.20bool.60.3F.html#217548865">(Nov 22 2020 at 12:23)</a>:</h4>
<p>Closures aren't callable, they go through the Fn trait methods</p>



<a name="217548877"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Is%20there%20some%20kind%20of%20%60is_callable%28ty%3A%20%26Ty%29%20-%3E%20bool%60%3F/near/217548877" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Is.20there.20some.20kind.20of.20.60is_callable(ty.3A.20.26Ty).20-.3E.20bool.60.3F.html#217548877">(Nov 22 2020 at 12:23)</a>:</h4>
<p>Though that might not matter here</p>



<a name="217556938"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Is%20there%20some%20kind%20of%20%60is_callable%28ty%3A%20%26Ty%29%20-%3E%20bool%60%3F/near/217556938" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Robin Moussu <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Is.20there.20some.20kind.20of.20.60is_callable(ty.3A.20.26Ty).20-.3E.20bool.60.3F.html#217556938">(Nov 22 2020 at 15:47)</a>:</h4>
<p>In C++, anything that have an <code>operator ()</code> is a callable. Functions (obviously), function pointers, functors (the C++ phrasing for an object with at least one <code>operator ()</code> member function, the equivalent of implementing an <code>Fn</code> trait for a struct in Rust) and lambda (the C++ name for closure) are callable. I'm surprise that closure are not considered callable in Rust.</p>



<a name="217557033"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Is%20there%20some%20kind%20of%20%60is_callable%28ty%3A%20%26Ty%29%20-%3E%20bool%60%3F/near/217557033" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Is.20there.20some.20kind.20of.20.60is_callable(ty.3A.20.26Ty).20-.3E.20bool.60.3F.html#217557033">(Nov 22 2020 at 15:49)</a>:</h4>
<p>Well, they kind of are, I think?</p>



<a name="217557084"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Is%20there%20some%20kind%20of%20%60is_callable%28ty%3A%20%26Ty%29%20-%3E%20bool%60%3F/near/217557084" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Is.20there.20some.20kind.20of.20.60is_callable(ty.3A.20.26Ty).20-.3E.20bool.60.3F.html#217557084">(Nov 22 2020 at 15:50)</a>:</h4>
<p>Which is perhaps a good indication that the <code>is_callable</code> method shouldn't exist</p>



<a name="217557107"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Is%20there%20some%20kind%20of%20%60is_callable%28ty%3A%20%26Ty%29%20-%3E%20bool%60%3F/near/217557107" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Is.20there.20some.20kind.20of.20.60is_callable(ty.3A.20.26Ty).20-.3E.20bool.60.3F.html#217557107">(Nov 22 2020 at 15:51)</a>:</h4>
<p>In MIR, only <code>FnPtr</code> and <code>FnDef</code> are callable</p>



<a name="217557162"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Is%20there%20some%20kind%20of%20%60is_callable%28ty%3A%20%26Ty%29%20-%3E%20bool%60%3F/near/217557162" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Is.20there.20some.20kind.20of.20.60is_callable(ty.3A.20.26Ty).20-.3E.20bool.60.3F.html#217557162">(Nov 22 2020 at 15:52)</a>:</h4>
<p>Calling a closure calls the <code>FnDef</code> of the corresponding <code>FnX</code> trait's <code>call_X</code> method</p>



<a name="217557742"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Is%20there%20some%20kind%20of%20%60is_callable%28ty%3A%20%26Ty%29%20-%3E%20bool%60%3F/near/217557742" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Robin Moussu <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Is.20there.20some.20kind.20of.20.60is_callable(ty.3A.20.26Ty).20-.3E.20bool.60.3F.html#217557742">(Nov 22 2020 at 16:07)</a>:</h4>
<p>So, <code>ty.is_fn() || ty.is_fn_ptr()</code> would be enough?</p>



<a name="217557884"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Is%20there%20some%20kind%20of%20%60is_callable%28ty%3A%20%26Ty%29%20-%3E%20bool%60%3F/near/217557884" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Is.20there.20some.20kind.20of.20.60is_callable(ty.3A.20.26Ty).20-.3E.20bool.60.3F.html#217557884">(Nov 22 2020 at 16:10)</a>:</h4>
<p>When looking at a call terminator in MIR, yes, but not when looking at the callee type of an HIR expression (which <em>can</em> indeed be a closure)</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>